package com.cameras;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Thread.State;

import android.widget.Toast;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import Encoder.ImageRuntimeException;
import Encoder.JpegEncoder;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class cameras extends Activity {

	private static final String TAG = "CameraDemo";
	Camera camera;
	Preview preview;
	//Button shutter;
	File picture;
	RadioRecord MRecord;
	Loadthread hthread;
	private File mySDcardDir;
	Bitmap mBitmap;
	BitmapFactory bitmapfactory;

	// media record variable
	private File myRecAudioFile;
	private MediaRecorder mMediaRecorder01;
	private boolean sdCardExit;
	int time = 3;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		sdCardExit = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);

		// get SD Card path
		if (sdCardExit)
			mySDcardDir = Environment.getExternalStorageDirectory();

		bitmapfactory = new BitmapFactory();

		preview = new Preview(this);
		((FrameLayout) findViewById(R.id.preview)).addView(preview);

		//shutter = (Button) findViewById(R.id.buttonClick);
		preview.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				preview.camera.takePicture(shutterCallback, rawCallback,
						jpegCallback);
				preview.camera.stopPreview();
				mBitmap = bitmapfactory.decodeFile(picture.getAbsolutePath());
				hthread = new Loadthread(preview.mHolder, mBitmap,time);
				MRecord = new RadioRecord(time, sdCardExit, mySDcardDir,
						preview.camera, hthread);
				
				MRecord.start();
				hthread.start();

				int[] pixce = new int[mBitmap.getWidth() * mBitmap.getHeight()];
				mBitmap.getPixels(pixce, 0, mBitmap.getWidth(), 0, 0,
						mBitmap.getWidth(), mBitmap.getHeight());
				mBitmap.recycle();
				System.gc();
				try {
					Thread Encoder = new JpegEncoder(pixce, 100, mBitmap.getWidth(),
							mBitmap.getHeight(), picture, myRecAudioFile ,MRecord );
					Encoder.start();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					//System.out.println("Encode Error");
					e.printStackTrace();
				} catch (ImageRuntimeException e) {
					// TODO Auto-generated catch block
					//System.out.println("Encode Error");
					e.printStackTrace();
				}

			}
		});

		Log.d(TAG, "onCreate'd");
	}

	ShutterCallback shutterCallback = new ShutterCallback() {
		public void onShutter() {
			Log.d(TAG, "onShutter'd");
		}
	};

	/** Handles data for raw picture */
	PictureCallback rawCallback = new PictureCallback() {
		public void onPictureTaken(byte[] data, Camera camera) {
			Log.d(TAG, "onPictureTaken - raw");
		}
	};

	/** Handles data for jpeg picture */
	PictureCallback jpegCallback = new PictureCallback() {
		public void onPictureTaken(byte[] data, Camera camera) {
			FileOutputStream outStream = null;
			try {
				// write to local sandbox file system
				// outStream =
				// CameraDemo.this.openFileOutput(String.format("%d.jpg",
				// System.currentTimeMillis()), 0);
				// Or write to sdcard
				picture = new File(String.format(mySDcardDir.getAbsolutePath()
						+ "%d.jpg", System.currentTimeMillis()));
				picture.createNewFile();
				outStream = new FileOutputStream(picture);
				// outStream = new FileOutputStream(String.format(
				// "/sdcard/%d.jpg", System.currentTimeMillis()));
				outStream.write(data);
				outStream.close();
				Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
			}
			Log.d(TAG, "onPictureTaken - jpeg");
		}
	};
}